try {
	if (i18n=="undefined") var i18n = "ko";
} catch(e) {
	var i18n = "ko";
}

function trim(st) {
	while(st) { if (st.indexOf(" ")==0) st = st.substring(1); else break; }
	while(st) { if (st.lastIndexOf(" ")==st.length-1) st = st.substring(0, st.length-1); else break; }
	return st;
}

function checkKeyword() {
	var keyword = document.getElementById("search-keyword").value;
	var myYoutube = document.getElementById("myYoutube");
	if(trim(keyword).length == 0 && myYoutube.disabled == false && myYoutube.value == ""){
		switch(i18n) {
			case "zh-TW":
				alert("請選擇欲搜索的項目與關鍵字。");
			break;
			case "zh-CN":
				alert("请选择欲搜索的项目与关键词。");
			break;
			case "en":
				alert("Please select item and input keyword which you want to search.");
			break;
			case "ko":
			default:
				alert("사용자 선택이나 검색어를 입력해 주세요.");
		}
		return false;
	}
	if(trim(keyword).length == 0 && myYoutube.disabled == true){
		switch(i18n) {
			case "zh-TW":
				alert("請輸入搜索關鍵字。");
			break;
			case "zh-CN":
				alert("请输入搜索关键词。");
			break;
			case "en":
				alert("Please input the keyword.");
			break;
			case "ko":
			default:
				alert("검색어를 입력해 주세요.");
		}
		return false;
	}
	return true;
}

function searchStart() {
	if(document.getElementById('searchErrorMsg')){
		document.getElementById('searchErrorMsg').style.display = "none";
	}
	document.getElementById('loadingBar').style.display = "block";
}

var selectedMediaURL = '';
function selectedThumbnail(item, mediaId, mediaName, mediaOwner){
	document.getElementById("iPreviewMedia").innerHTML = document.getElementById("iPreviewMedia-"+mediaId).innerHTML;
	document.getElementById("iMediaTitle").innerHTML = document.getElementById("iMediaTitle-"+mediaId).innerHTML;
	document.getElementById("iMediaName").innerHTML = document.getElementById("iMediaName-"+mediaId).innerHTML;
	document.getElementById("iMediaDate").innerHTML = document.getElementById("iMediaDate-"+mediaId).innerHTML;
	document.getElementById("iMediaViewCount").innerHTML = document.getElementById("iMediaViewCount-"+mediaId).innerHTML;
	if (mediaName == 'youtube') {
		document.getElementById("iMediaTime").innerHTML = document.getElementById("iMediaTime-"+mediaId).innerHTML;
	} else if (mediaName == 'flickr') {
		document.getElementById("iPhotoId").value = mediaId;
		document.getElementById("iMediaLicense").innerHTML = document.getElementById("iMediaLicense-"+mediaId).innerHTML;
		var photoSizes = document.getElementById("iMediaSize");
	}

	var items = document.getElementById("mediaSearchListId").getElementsByTagName("li");
	for(var i=0;i<items.length;i++){
		items[i].className = "imgThumbnail";
	}
	item.className = "imgThumbnailSelected";

	if (mediaName == 'youtube') {
		selectedMediaURL = "http://www.youtube.com/v/" + mediaId;
	} else if (mediaName == 'flickr') {
		var requestURL = blogURL + "/plugin/FlickrPhotoSizeView/?photoid=" + mediaId;
		var request = new HTTPRequest("GET", requestURL);
		request.onSuccess = function () {
			photoSizes.innerHTML = this._request.responseText;
		}
		request.onVerify = function () {
			return true;
		}
		request.send();
		selectedMediaURL = "http://www.flickr.com/photos/" + mediaOwner + "/" + mediaId;
	}
}

function mediaInsert(mediaName){
	if(selectedMediaURL != ''){

		var isWYSIWYG = false;
		try{
			if(window.parent.editor.editMode == 'WYSIWYG'){
				isWYSIWYG = true;
			}
		}
		catch(e){ }		

		var mForm = document.previewForm;
		var mAligns = mForm.insertAlign;
		var mAlign = "";
		var mAlignOption = "";
		for(var i = 0; i < mAligns.length; i++)  {
			if(mAligns[i].checked == true)
			{
				mAlign = mAligns[i].value;
				break; 
			}
		}
		
		switch(mAlign) {
			case 'left':
				mAlignOption = ' class="imageblock left" style="float: left; margin-right: 10px;" ';
				break;
			case 'center':
				mAlignOption = ' class="imageblock center" style="text-align: center; clear: both;" ';
				break;
			case 'right':
				mAlignOption = ' class="imageblock right" style="float: right; margin-left: 10px;" ';
				break;
		}

		if (mediaName == 'youtube') {
			var mediaCode = '<div ' + mAlignOption + '><object width="425" height="355"><param name="movie" value="'+selectedMediaURL+'"></param><param name="wmode" value="transparent"></param><embed src="'+selectedMediaURL+'" type="application/x-shockwave-flash" wmode="transparent" width="425" height="355"></embed></object></div>';
			if (isWYSIWYG) {
				window.parent.editor.command("Raw", '<img class="tatterObject" src="' + servicePath + adminSkin + '/image/spacer.gif"' + window.parent.editor.parseImageSize(mediaCode, "string", "css") + ' longDesc="' + window.parent.editor.objectSerialize(mediaCode) + '" />', "");
			} else {
				window.parent.insertTag(window.parent.editor.textarea, mediaCode,"");
			}
		} else if (mediaName == 'flickr') {
			var mSize = mForm.photoSize.value;
			var photoId = mForm.photoId.value;
			var linkTarget = mForm.linkTarget.value;
			linkTarget = (linkTarget=='blank' ? ' target="_blank" ' : '');
			var imageSrc = document.getElementById(mSize + "_" + photoId).value;
			var mediaCode = '<div ' + mAlignOption + '><a href="' + selectedMediaURL + '" ' + linkTarget + '><img src="' + imageSrc + '" border="0" /></a></div>';
			if (isWYSIWYG) {
				window.parent.editor.command("Raw", mediaCode, "");
			} else {
				window.parent.insertTag(window.parent.editor.textarea, mediaCode, "");
			}
		}
		window.parent.dlg.hide();
		window.parent.dlg.setContent('');
	} else {
		switch(i18n) {
			case "zh-TW":
				var errString = (mediaName == 'youtube' ? '動畫' : '影像');
				alert("請選擇要插入的 " + errString);
			break;
			case "zh-CN":
				var errString = (mediaName == 'youtube' ? '动画' : '影像');
				alert("请选择要插入的 " + errString);
			break;
			case "en":
				var errString = (mediaName == 'youtube' ? 'a video' : 'an image');
				alert("Please select " + errString + " which you want to insert into the article.");
			break;
			case "ko":
			default:
				var errString = (mediaName == 'youtube' ? '동영상을' : '이미지를');
				alert("삽입할 " + errString + " 선택하세요.");
		}
	}
	return false;
}

function cancel(){
	window.parent.dlg.hide();
	window.parent.dlg.setContent('');
	return false;
}

function selectUserCheck(obj){
	if(obj.value == ""){
		obj.options[0].selected = true;
	}
}

function userSetsView(person){
	var responseText = "";
	var request = "";
	var userSetsList = document.getElementById("userSetsList");
	var loadingSelect =  document.getElementById("loadingSelect");
	var requestURL = blogURL + "/plugin/FlickrUserSetsView/?person=" + person;
	userSetsList.innerHTML = loadingSelect.innerHTML;
	request = new HTTPRequest("GET", requestURL);
	request.onSuccess = function () {
		responseText = this._request.responseText;
		if(responseText){
			userSetsList.innerHTML = responseText;
			userSetsDisabledCheck(person);
			elementFadeUp('userSets',44,158,197);
		}else{
			userSetsList.innerHTML = "";
			FlickrDisabledSet(false);
		}
	}
	request.onError = function (){
		return false;
	}
	request.onVerify = function (){
		return true;
	}
	request.send();
}

function userSetsDisabledCheck(person){
	var userSets = document.getElementById("userSets");
	if(userSets){
		if(person != "all"){
			userSets.disabled = false;
			if(userSets.value == ""){
				FlickrDisabledSet(false);
			}else{
				FlickrDisabledSet(true);
			}
		}else{
			userSets.disabled = true;
			FlickrDisabledSet(false);
		}
	}
}


function userSetsCheck(obj){
	var userSets = document.getElementById("userSets");
	selectUserCheck(obj);
	if(obj.value != "all"){
		 userSetsView(obj.value);
	}else{
		if(userSets){
			userSets.style.display = 'none';
			userSets.disabled = true;
			FlickrDisabledSet(false);
		}
	}
}

function userSetsSelect(obj){
	if(obj.value == ""){
		FlickrDisabledSet(false);
	}else{
		FlickrDisabledSet(true);
	}
}

function FlickrDisabledSet(mode){
	if(mode == false){
		document.getElementById("search-keyword").disabled = false;
		document.getElementById("orderbyNone").disabled = false;
		document.getElementById("orderbyPublished").disabled = false;
		document.getElementById("orderbyInterestingness").disabled = false;
	}else{
		document.getElementById("search-keyword").disabled = true;
		document.getElementById("orderbyNone").disabled = true;
		document.getElementById("orderbyPublished").disabled = true;
		document.getElementById("orderbyInterestingness").disabled = true;
	}
}

function elementFadeUp(elementId,red,green,blue) {

	if (red ==255 && green == 255 && blue==255) {
		document.getElementById(elementId).style.backgroundColor = "#ffffff";
		return true;
	}

	document.getElementById(elementId).style.backgroundColor = "rgb("+red+","+green+","+blue+")";

	red = red + Math.ceil((255 - red)/10);
	green = green + Math.ceil((255-green)/10);
	blue = blue + Math.ceil((255-blue)/10);

	setTimeout("elementFadeUp('"+elementId+"',"+red+","+green+","+blue+")", 30);

}